﻿<link href="~/RoadFlowResources/scripts/bigcolorpicker/css/jquery.bigcolorpicker.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="~/RoadFlowResources/scripts/bigcolorpicker/js/jquery.bigcolorpicker.js"></script>
<script type="text/javascript" src="~/RoadFlowResources/scripts/bigcolorpicker/js/syntaxHighlighter/shCore.js"></script>
<script type="text/javascript" src="~/RoadFlowResources/scripts/bigcolorpicker/js/syntaxHighlighter/shBrushJScript.js"></script>
<script type="text/javascript" src="~/RoadFlowResources/scripts/bigcolorpicker/js/syntaxHighlighter/shBrushJava.js"></script>
<link href="~/RoadFlowResources/scripts/bigcolorpicker/css/syntaxHighlighter/shCore.css" rel="stylesheet" type="text/css" />
<link href="~/RoadFlowResources/scripts/bigcolorpicker/css/syntaxHighlighter/shCoreDefault.css" rel="stylesheet" type="text/css" />
<link href="~/RoadFlowResources/scripts/bigcolorpicker/css/syntaxHighlighter/shThemeDefault.css" rel="stylesheet" type="text/css" />

<form method="post">
    <div id="tabdiv">
        <div id="div_base" title="基本信息">
            <div style="height:8px;"></div>
            <table cellpadding="0" cellspacing="1" border="0" width="99%" class="formtable">
                <tr>
                    <th style="width:100px;">流程ID：</th>
                    <td><input type="text" id="base_ID" name="base_ID" readonly="readonly" value="@ViewData["flowId"]" class="mytext" style="width:75%" /></td>
                </tr>
                <tr>
                    <th>流程名称：</th>
                    <td><input type="text" id="base_Name" name="base_Name" class="mytext" style="width:75%" /></td>
                </tr>
                <tr>
                    <th>流程分类：</th>
                    <td>
                        <select id="base_Type" name="base_Type" class="myselect"><option value=""></option>@Html.Raw(ViewData["flowTypeOptions"])</select>
                    </td>
                </tr>
                <tr>
                    <th>管理者：</th>
                    <td><input type="text" id="base_Manager" opener="parent" value="@ViewData["defaultManager"]" name="base_Manager" class="mymember" title="选择流程管理者" more="1" user="1" dept="0" station="0" workgroup="0" unit="0" style="width:75%" /></td>
                </tr>
                <tr>
                    <th>实例管理者：</th>
                    <td><input type="text" id="base_InstanceManager" opener="parent" value="@ViewData["defaultManager"]" name="base_InstanceManager" class="mymember" title="选择流程实例管理者" more="1" user="1" dept="0" station="0" workgroup="0" unit="0" style="width:75%" /></td>
                </tr>
                <tr>
                    <th>调试模式：</th>
                    <td>
                        <select class="myselect" id="base_Debug">
                            <option value="0">关闭</option>
                            <option value="1">开启(有调试窗口)</option>
                            <option value="2">开启(无调试窗口)</option>
                        </select>
                        <input type="text" id="base_DebugUsers" title="选择调试人员" opener="parent" class="mymember" /> //调试人员
                    </td>
                </tr>
                <tr>
                    <th>图标：</th>
                    <td>
                        <input type="text" id="base_ico" opener="parent" class="myico" />
                        &nbsp;&nbsp;颜色：<input type="text" id="base_color" name="base_color" value="" class="mytext" style="width:150px" />
                    </td>
                </tr>
                <tr>
                    <th>备注：</th>
                    <td><textarea rows="1" cols="1" id="base_Note" name="base_Note" class="mytext" style="width:90%; height:30px;"></textarea></td>
                </tr>
            </table>
        </div>
        <div id="div_data" title="数据连接">
            <div style="height:8px;"></div>
            <table cellpadding="0" cellspacing="1" border="0" width="99%" style="width:99%" class="listtable" id="link_listtable">
                <thead>
                    <tr>
                        <th style="width:28%">数据库连接</th>
                        <th style="width:28%">数据表</th>
                        <th style="width:30%">主键</th>
                        <th><a href="javascript:link_add();"><i class="fa fa-plus" style="margin-right:4px;"></i><span>添加</span></a></th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
        <div id="div_title" title="标识字段">
            <div style="height:8px;"></div>
            <table cellpadding="0" cellspacing="1" border="0" width="99%" class="formtable">
                <tr>
                    <th style="width:100px;">数据连接：</th>
                    <td>
                        <select id="title_dbconn" name="title_dbconn" class="myselect"
                                onchange="title_db_change(this)" style="width:400px;">
                            <option value=""></option>
                            @Html.Raw(ViewData["dbconnOptions"])
                        </select>
                    </td>
                </tr>
                <tr>
                    <th>数据表：</th>
                    <td><select id="title_tables" onchange="title_table_change(this)" name="title_tables" class="myselect2" style="width:400px;"><option value=""></option></select></td>
                </tr>
                <tr>
                    <th>完成标识：</th>
                    <td><select id="title_title" name="title_title" class="myselect2" style="width:400px;"><option value=""></option></select></td>
                </tr>
            </table>
        </div>
    </div>
    <div style="width:99%; margin:8px auto 0 auto; text-align:center;">
        <input type="button" class="mybutton" value=" 确 定 " onclick="confirm1(this)" />
        <input type="button" class="mybutton" value=" 取 消 " onclick="new RoadUI.Window().close();" />
    </div>
</form>
<script type="text/javascript">
    var link_options = '<option value=""></option>@Html.Raw(ViewData["dbconnOptions"])'; //数据连接选项
    var isAdd = '1' == '@ViewData["isAdd"]';
    var openerid = '@ViewData["openerid"]';
    var flowID = '@ViewData["flowId"]';
    var defaultManager = '@ViewData["defaultManager"]';
    var win = new RoadUI.Window();
    var frame = null;
    $(function () {
        $("#base_color").bigColorpicker();
        new RoadUI.Tab({ id: "tabdiv", replace: true, contextmenu: false, dblclickclose: false });
        var iframes = top.frames;
        for (var i = 0; i < iframes.length; i++) {
            var fname = "";
            try {
                fname = iframes[i].name;
            }
            catch (e) {
                fname = "";
            }
            if (fname == openerid + "_iframe") {
                frame = iframes[i];
                break;
            }
        }
        if(frame == null){
            frame = top.roadflowCurrentWindow;
        }
        if (frame == null) {
            alert("未找到父窗口,设置将不可用!");
            return;
        };
        if (!isAdd) {
            var json = frame.wf_json;
            if (json) {
                $("#base_Name").val(json.name);
                $("#base_Type").val(json.type);
                $("#base_Manager").val(json.manager || defaultManager);
                new RoadUI.Member().setValue($("#base_Manager"));
                $("#base_InstanceManager").val(json.instanceManager || defaultManager);
                new RoadUI.Member().setValue($("#base_InstanceManager"));
                $("#base_RemoveCompleted").val(json.removeCompleted);
                $("#base_Note").val(json.note);
                $("#base_Debug").val(json.debug);
                $("#base_DebugUsers").val(json.debugUsers);
                $("#base_ico").val(json.ico || "");
                $("#base_color").val(json.color || "");
                new RoadUI.Member().setValue($("#base_DebugUsers"));
                var databases = json.databases;
                if (databases) {
                    for (var i = 0; i < databases.length; i++) {
                        link_add(databases[i].link, databases[i].table, databases[i].primaryKey);
                    }
                }
                if (json.titleField) {
                    var titleFieldLink=json.titleField.link||"";
                    if(titleFieldLink.length==0 && databases && databases.length>0){
                        titleFieldLink=databases[0].link;
                    }
                    $("#title_dbconn").val(titleFieldLink);
                    $("#title_tables").html(frame.getTables(json.titleField.link, json.titleField.table));
                    $("#title_title").html('<option value=""></option>' + frame.getFields(json.titleField.link, json.titleField.table, json.titleField.field));
                }
            }
        }
    });

    function link_add(db,table,field) {
        var tableOptions = '';
        var fieldOptions = '';
        if (db && table) {
            tableOptions = frame.getTables(db, table);
        }
        if (db && table && field) {
            fieldOptions = frame.getFields(db, table, field);
        }
        var index = $("#link_listtable tbody tr").size() + 1;
        var tr = '<tr>';
        tr += '<td style="background:#ffffff; height:30px;">';
        tr += '<input type="hidden" name="link_index" value="' + index.toString() + '"/>';
        tr += '<select class="myselect" style="width:120px" onchange="link_db_change(this);" id="link_db_' + index.toString() + '" name="link_db_' + index.toString() + '">' + link_options + '</select></td>';
        tr += '<td style="background:#ffffff;"><select class="myselect2" dropdownwidth="300px" style="width:120px" onchange="link_table_change(this)" id="link_table_' + index.toString() + '" name="link_table_' + index.toString() + '">' + tableOptions + '</select></td>';
        tr += '<td style="background:#ffffff;"><select class="myselect2" dropdownwidth="220px" style="width:120px" id="link_key_' + index.toString() + '" name="link_key_' + index.toString() + '">' + fieldOptions + '</select></td>';
        tr += '<td style="background:#ffffff;"><a href="javascript:link_delete(' + index.toString() + ');"><i class="fa fa-times" style="margin-right:4px;"></i>删除</a></td>';
        tr += '</tr>';
        $("#link_listtable tbody").append(tr);
        new RoadUI.Select().init($(".myselect", $("#link_listtable tbody")));
        new RoadUI.Select().init2($(".myselect2", $("#link_listtable tbody")));
        if (db) {
            $("#link_db_" + index.toString()).val(db);
        }
    }
    function link_delete(index) {
        $("#link_listtable tbody tr td input[type='hidden']").each(function () {
            if ($(this).val() == index.toString()) {
                $(this).parent().parent().remove();
            }
        });
    }
    function link_db_change(obj, table)
    {
        if (!obj || !obj.value) return;
        $("#link_listtable tbody tr").each(function ()
        {
            var $dbselect = $("[id^='link_db_']", $(this));
            if ($dbselect.size() > 0)
            {
                $dbselect.val(obj.value);
            }
        });
        var html = '<option value=""></option>' + frame.getTables(obj.value, table);
        $("select", $(obj).parent().next()).html(html);
        var title_tables = $("#title_tables").val();
        var title_title = $("#title_title").val();
        //$("#title_dbconn").val(obj.value);
        //var $table = $("[id^='link_table_']",$("#link_listtable tbody tr:first"));
        //if($table.size()>0) {
        //    $("#title_tables").html(frame.getTables(obj.value, $table.val()));
        //}
    }

    function link_table_change(obj, field)
    {
        if (!obj || !obj.value) return;
        var conn = $("select", $(obj).parent().prev()).val();
        var html = '<option value=""></option>' + frame.getFields(conn, obj.value, field);
        $("select", $(obj).parent().next()).html(html);
        //var $table = $("[id^='link_table_']",$("#link_listtable tbody tr:first"));
        //if($table.size()>0) {
        //    $("#title_tables").html(frame.getTables(conn, $table.val()));
        //    title_table_change($("#title_tables").get(0), $table.val());
        //}

    }

    function title_db_change(obj, table)
    {
        if (!obj || !obj.value) return;
        $("#title_tables").html('<option value=""></option>' + frame.getTables(obj.value, table));

    }
    function title_table_change(obj, fields)
    {
        if (!obj || !obj.value) return;
        var conn = $("#title_dbconn").val();
        $("#title_title").html(frame.getFields(conn, obj.value, fields));
    }

    function confirm1(but)
    {
        if (($("#base_Name").val() || '').length == 0)
        {
            alert("流程名称不能为空!");
            return;
        }
        if (($("#base_Type").val() || '').length == 0)
        {
            alert("流程分类不能为空!");
            return;
        }
        $(but).prop("disabled", true);
        if (isAdd)
        {
            frame.initwf();
        }
        var json = frame.wf_json;
        json.id = flowID;
        json.name = $("#base_Name").val() || '';
        json.type = $("#base_Type").val() || '';
        json.manager = $("#base_Manager").val() || '';
        json.instanceManager = $("#base_InstanceManager").val() || '';
        json.removeCompleted = $("#base_RemoveCompleted").val() || '';
        json.debug = $("#base_Debug").val() || "0";
        json.debugUsers = $("#base_DebugUsers").val() || '';
        json.note = $("#base_Note").val() || '';
        json.ico = $("#base_ico").val() || '';
        json.color = $("#base_color").val() || '';
        json.databases = [];
        $("input[type='hidden'][name='link_index']").each(function ()
        {
            var index = $(this).val();
            json.databases.push({
                link: $('#link_db_' + index).val() || '',
                linkName: $("#link_db_" + index + " option[value='" + ($('#link_db_' + index).val() || '') + "']").text(),
                table: $('#link_table_' + index).val() || '',
                primaryKey: $('#link_key_' + index).val() || ''
            });
        });
        json.titleField = {
            link: $("#title_dbconn").val() || '',
            table: $("#title_tables").val() || '',
            field: $("#title_title").val() || ''
        };
        frame.wf_id = flowID;
        frame.init_links_tables_fields(json.databases, json.titleField);
        new RoadUI.Window().close();
    }
</script>